package com.blogSystem.article.service;

import com.blogSystem.article.dao.ArticleDao;
import com.blogSystem.article.dao.CommentMapper;
import com.blogSystem.article.dao.CommentThumbsMapper;
import com.blogSystem.article.pojo.Comment;
import com.blogSystem.article.pojo.CommentThumbs;
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.xml.transform.Result;
import java.util.List;

@Service
@Transactional
public class CommentThumbsService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private CommentThumbsMapper commentThumbsMapper;


    public void  addCommentThumbs(CommentThumbs commentThumbs){
        commentThumbs.setId(idWorker.nextId()+"");
        commentThumbsMapper.insert(commentThumbs);
        Comment comment=commentMapper.selectById(commentThumbs.getComment_id());
        if(comment!=null){
            comment.setThumbs(comment.getThumbs()+1);
            commentMapper.update(comment);
        }
    }
    public void deleteCommentThumbs(String user_id,String comment_id){
        CommentThumbs commentThumbs=commentThumbsMapper.selectByUserIdAndCommentId(comment_id,user_id);
        if(commentThumbs!=null){
            commentThumbsMapper.deleteById(commentThumbs.getId());
            Comment comment=commentMapper.selectById(comment_id);
            if(comment!=null){
                comment.setThumbs(comment.getThumbs()-1);
                commentMapper.update(comment);
            }
        }


    }
    public CommentThumbs findThumbs(String comment_id,String user_id){
        return commentThumbsMapper.selectByUserIdAndCommentId(comment_id,user_id);
    }

    public void deleteByArticleId(String article_id){
        List<Comment> comments=commentMapper.selectParentByArticleId(article_id);
        if(comments!=null&&!comments.isEmpty()){
            List <String> ids=null;
            List <String> comment_ids=null;
            for(Comment comment:comments){
                comment_ids.add(comment.getId());
            }
            List <CommentThumbs> commentThumbsList=commentThumbsMapper.selectByCommentIds(comment_ids);
            if(commentThumbsList!=null&&!commentThumbsList.isEmpty()){
                for(CommentThumbs commentThumbs:commentThumbsList){
                    ids.add(commentThumbs.getId());
                }
                commentThumbsMapper.deleteByIds(ids);
            }

        }



    }


}
